package com.wpp;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/*
 * 操作数据库的工具类
 *
 *
 * */
public class JDBCUtils {

    public static Connection getConnection() throws Exception {
        Connection connection = null;
        PreparedStatement pis = null;
        //获取配置信息
        //加载进流
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);

        //读取属性
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");

        //加载驱动
        Class.forName(driver);

        //获取数据库连接
        connection = DriverManager.getConnection(url, user, password);

        return connection;
    }

    //关闭连接和关闭statement操作
    public static void closeResource(Connection connection, Statement pis) {
        try {
            if (pis != null) {

                pis.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (connection != null) {

                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
